package com.xigua.xcar.net;

import java.util.List;

import org.apache.http.NameValuePair;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/*
case 0:
		{
			asyncReqListByNameParser(errorcode, data, status);
		}break;
		
		case 1:	//加油站
		{
			asyncReqListByTypeParser(errorcode, data, status, 1);
		}break;
		
		case 2:	//洗车
		{
			asyncReqListByTypeParser(errorcode, data, status, 2);
		}break;
		
		case 3:	//详细
		{
			asyncReqDetailParser(errorcode, data, status);
		}break;
*/

public abstract class AbstractDataManager {
	public static final int RESULTCODE_SUCCESS = 0;
	public static final int RESULTCODE_NETERR = 1;
	public static final int RESULTCODE_OTHERERR = 2;
	public abstract List<NameValuePair> getRequestParam();
	public abstract AbstractData afterParseData(String data);
	public abstract AbstractData getData();
	private static final String tag = "AbstractDataManager";
	private OnlineAppHttpRequest m_httpRequest;
	protected Context mContext;
	
	private OnlineAppHttpRequestListener mListener = new OnlineAppHttpRequestListener() {
		
		@Override
		public boolean onHttpRequestStart(int resultCode) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean onHttpRequestError(int resultCode) {
			Log.i(tag, "onHttpRequestError");
			if(null != mHandler){
				Message msg = Message.obtain();
	    		msg.what = RESULTCODE_NETERR;
	    		msg.obj = "网络异常";
	    		mHandler.sendMessage(msg);
	    	}
			return false;
		}
		
		@Override
		public boolean onHttpRequestComplete(int resultCode, int serverRespCode,
				String response) {
			Log.i(tag, "onHttpRequestComplete");
			String asyncPostMessage;
			if(OnlineAppHttpRequestListener.HTTP_RESULT_OK == resultCode){
				Message msg = Message.obtain();
		    	if(null != response)
		    	{
		    		asyncPostMessage = response;
		    	}else{
		    		asyncPostMessage = null;
		    	}
		    	if(null != mHandler){
		    		msg.what = RESULTCODE_SUCCESS;
		    		msg.obj = afterParseData(asyncPostMessage);
		    		mHandler.sendMessage(msg);
		    	}
			}else{
				if(null != mHandler){
					Message msg = Message.obtain();
		    		msg.what = RESULTCODE_NETERR;
		    		msg.obj = "网络异常";
		    		mHandler.sendMessage(msg);
		    	}
			}
	    	
	    	return true;
		}
		
		@Override
		public boolean onHttpRequestCancle(int resultCode) {
			return false;
		}
	};
	private Handler mHandler = null;
	
	public AbstractDataManager(Context context){
		mContext = context;
	}
	
	public void setHanlder(Handler handler){
		mHandler = handler;
	}
	
	public void getDataFromServer(){
		m_httpRequest = new OnlineAppHttpRequest(mContext, mListener);
		List<NameValuePair> postParamList = getRequestParam();
		m_httpRequest.beginRequest(getUrl(), OnlineAppHttpRequest.METHOD_POST, 0, 0, postParamList);
	}
	
	public void cancelRequest(){
		if(null != m_httpRequest){
			m_httpRequest.endRequest();
		}
	}
	
	public String getUrl(){
//		return "http://goandsee.tk/icar/db/queryInterface.php";
//		return "http://31.170.166.189/icar/db/queryInterface.php";
		return "http://1.xcar2012.sinaapp.com/db/queryInterface.php";
	}
}
